//
//  NewTaskFooter.m
//  RRQS
//
//  Created by kyjun on 16/3/24.
//
//

#import "NewTaskFooter.h"

@interface NewTaskFooter ()<AMapSearchDelegate>
@property(nonatomic,strong) UIView* topView;
@property(nonatomic,strong) UIButton* btnIcon;
@property(nonatomic,strong) UILabel* labelSendAddress;
@property(nonatomic,strong) TTTAttributedLabel* labelReGeocode;
@property(nonatomic,strong) UIView* bottomView;
@property(nonatomic,strong) UIButton* btnRush;
@property(nonatomic,strong) UIButton* btnRob;

@property (nonatomic, strong) AMapSearchAPI *search;
@property(nonatomic,strong) UILabel* labelDistance;
@property(nonatomic,strong) UIView* lineView;
@property(nonatomic,strong) TTTAttributedLabel* remarkLabel;
@property(nonatomic,strong) UIButton* addressIcon;

@end

@implementation NewTaskFooter


-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        self.backgroundColor = [UIColor colorWithRed:255/255.f green:255/255.f blue:255/255.f alpha:1.0];
        [self layoutUI];
        [self layoutConstraints];
    }
    return self;
}


#pragma mark =====================================================  user inferface layout
-(void)layoutUI{
    [self addSubview:self.topView];
    [self addSubview:self.lineView];
    [self addSubview:self.bottomView];

    [self.topView addSubview:self.btnIcon];
    [self.topView addSubview:self.labelSendAddress];
    [self.topView addSubview:self.labelReGeocode];
    [self.bottomView addSubview:self.btnRush];
    [self.bottomView addSubview:self.btnRob];

    [self.topView addSubview:self.labelDistance];
    [self.topView addSubview:self.remarkLabel];

    [self.topView addSubview:self.addressIcon];

}

-(void)layoutConstraints{
    
    
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(60);
        make.left.right.equalTo(self);
        make.bottom.equalTo(self);
    }];
    
    [self.btnRush mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bottomView).offset(10);
        make.left.equalTo(self.bottomView).offset(10);
        make.right.equalTo(self.bottomView).offset(-10);
        make.bottom.equalTo(self.bottomView).offset(-10);
    }];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.bottomView.mas_top);
        make.left.right.equalTo(self);
        make.height.mas_equalTo(0.8);
    }];
    
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self);
        make.left.right.equalTo(self);
        make.bottom.equalTo(self.lineView.mas_top);
    }];
    
    [self.btnIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.topView);
        make.size.mas_equalTo(CGSizeMake(25, 25));
        make.left.equalTo(self.topView).offset(10);
    }];
    
//    [self.labelDistance mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerY.equalTo(self.topView);
//        make.size.mas_equalTo(CGSizeMake(100, 30));
//        make.right.equalTo(self.topView).offset(-10);
//    }];
    
    [self.addressIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.topView);
        make.height.width.mas_equalTo(25);
        make.right.equalTo(self.topView).offset(-10);
    }];
    
    
    [self.labelSendAddress mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.topView);
        make.left.equalTo(self.btnIcon.mas_right).offset(10);
        make.right.equalTo(self.addressIcon.mas_left);
        make.height.equalTo(self.topView).multipliedBy(0.5);
        
    }];
    
    [self.remarkLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(20);
        make.left.equalTo(self.labelSendAddress);
        make.right.equalTo(self.labelSendAddress);
        make.bottom.equalTo(self.topView);
        
    }];
    
    [self.labelReGeocode mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.labelSendAddress.mas_bottom);
        make.left.equalTo(self.labelSendAddress);
        make.right.equalTo(self.labelSendAddress);
        make.bottom.equalTo(self.remarkLabel.mas_top);
        
    }];
    
}

/* 逆地理编码回调. */
- (void)onReGeocodeSearchDone:(AMapReGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response
{
    if (response.regeocode != nil)
    {
        self.labelReGeocode.text = response.regeocode.formattedAddress;
    }
}

#pragma mark =====================================================  SEL
-(void)grabOneTouch:(UIButton*)sender{
//    if(self.delegate && [self.delegate respondsToSelector:@selector(NewTaskGrabOne:)]){
//        [self.delegate NewTaskGrabOne:self.entity];
//    }
    if (self.isWaimOrPaot) {
        if(self.delegate && [self.delegate respondsToSelector:@selector(NewTaskPaoTui:)]){
            [self.delegate NewTaskPaoTui:self.waiMaiDodelHList];
        }
    }else{
        if(self.delegate && [self.delegate respondsToSelector:@selector(NewTaskWaiMai:)]){
            [self.delegate NewTaskWaiMai:self.waiMaiDodelList];
        }
    }
    
}
-(void)robOrderTouch:(UIButton*)sender{

    if(self.delegate && [self.delegate respondsToSelector:@selector(NewTaskRobOrder:)]){
        [self.delegate NewTaskRobOrder:self.waiMaiDodelList];
    }
}



#pragma mark =====================================================  property package
- (void)setWaiMaiDodelHList:(WJWaiMaiDodelHList *)waiMaiDodelHList // 跑腿
{

    self.btnRob.hidden = YES;

    if(waiMaiDodelHList){
        
        self.isWaimOrPaot = YES;
        _waiMaiDodelHList = waiMaiDodelHList;
//        self.labelSendAddress.text = waiMaiDodelHList.send_address;
        self.labelReGeocode.text =  waiMaiDodelHList.map_location;
        self.labelSendAddress.text = [NSString stringWithFormat:@"%@/%@",waiMaiDodelHList.user_name,waiMaiDodelHList.user_phone];

        
        if (waiMaiDodelHList.order_mark.length != 0) {
            self.remarkLabel.hidden = NO;
            self.remarkLabel.text =  [NSString stringWithFormat:@"备注：%@",waiMaiDodelHList.order_mark];
        }else{
            self.remarkLabel.hidden = YES;

        }

        
        NSString *stra = [WMHelper getTheCorrectNum:waiMaiDodelHList.location_get];
        if (stra.length == 0) {
            
            _labelDistance.text = @"送货";
            
        }else{
            
            NSString *strMoney = [NSString stringWithFormat:@"%@km送货",stra];
            NSMutableAttributedString *attributeStr  = [[NSMutableAttributedString alloc]initWithString:strMoney];
            [attributeStr addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12.f],NSForegroundColorAttributeName:[UIColor colorWithRed:248/255.f green:159/255.f blue:39/255.f alpha:1.0]} range:[strMoney rangeOfString:[NSString stringWithFormat:@"%@km",stra]]];
            _labelDistance.attributedText = attributeStr;
            
        }
        
        self.btnRob.hidden = YES;
        
        [self.btnRush mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.bottomView).offset(10);
            make.left.equalTo(self.bottomView).offset(10);
            make.right.equalTo(self.bottomView).offset(-10);
            make.bottom.equalTo(self.bottomView).offset(-10);
        }];
        
        if ([waiMaiDodelHList.type isEqualToString:@"buy"]) {
            [_btnRush setTitle:@"帮你买" forState:UIControlStateNormal];
        }
        if ([waiMaiDodelHList.type isEqualToString:@"send"]) {
            [_btnRush setTitle:@"帮你送" forState:UIControlStateNormal];
        }
        if ([waiMaiDodelHList.type isEqualToString:@"get"]) {
            [_btnRush setTitle:@"帮你取" forState:UIControlStateNormal];
        }
        
    }
}
- (void)setWaiMaiDodelList:(WJWaiMaiDodelList *)waiMaiDodelList // 外卖
{

    if(waiMaiDodelList){
        self.isWaimOrPaot = NO;
        _waiMaiDodelList = waiMaiDodelList;
        self.labelSendAddress.text = [NSString stringWithFormat:@"%@/%@",waiMaiDodelList.user_name,waiMaiDodelList.user_phone];
        self.labelReGeocode.text =  waiMaiDodelList.address_road;
        if (waiMaiDodelList.order_mark.length != 0) {
            self.remarkLabel.hidden = NO;
            self.remarkLabel.text =  [NSString stringWithFormat:@"备注：%@",waiMaiDodelList.order_mark];
        }else{
            self.remarkLabel.hidden = YES;
        }
        
        NSString *type ;
        if ([waiMaiDodelList.dingdantype integerValue] == 1 ) {
            
            type = @"系统单";
        }else{
            type = @"调度单";
        }
    //判断是不是系统单 如果是系统单显示 确认和抢单按钮   如果是人工单 只显示确认按钮
        if ([waiMaiDodelList.zhuandan integerValue] == 1 ) {
            
            [self.btnRush mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.bottomView).offset(10);
                make.left.equalTo(self.bottomView).offset(10);
                make.bottom.equalTo(self.bottomView).offset(-10);
                make.width.equalTo(self.bottomView.mas_width).multipliedBy(0.65);

            }];
            
            [self.btnRob mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.bottomView).offset(10);
                make.left.equalTo(self.btnRush.mas_right).offset(10);
                make.right.equalTo(self.bottomView).offset(-10);
                make.bottom.equalTo(self.bottomView).offset(-10);
            }];
            
            
            [_btnRush setTitle:[NSString stringWithFormat:@"确认(%@)",type] forState:UIControlStateNormal];
            [self.btnRob setTitle:@"转单" forState:UIControlStateNormal];

            self.btnRob.hidden = NO;

        }else{
            
            self.btnRob.hidden = YES;
            
            [self.btnRush mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.bottomView).offset(10);
                make.left.equalTo(self.bottomView).offset(10);
                make.right.equalTo(self.bottomView).offset(-10);
                make.bottom.equalTo(self.bottomView).offset(-10);
            }];
            
            [_btnRush setTitle:[NSString stringWithFormat:@"确认(%@)",type] forState:UIControlStateNormal];

        }

    }
}

-(void)setEntity:(MOrder *)entity{
    if(entity){
        _entity = entity;
        MSubOrder* item = (MSubOrder*)[entity.arraySubOrder firstObject];
        self.labelSendAddress.text = entity.address;
        if([WMHelper isEmptyOrNULLOrnil:entity.addressRoad]){
            if([WMHelper isEmptyOrNULLOrnil:item.phoneAddress]){
                self.labelReGeocode.text =  @"";
            }else{
                NSArray* empty = [item.phoneAddress componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@","]];
                if(empty.count == 2){
                    AMapReGeocodeSearchRequest *regeo = [[AMapReGeocodeSearchRequest alloc] init];
                    regeo.location = [AMapGeoPoint locationWithLatitude:[[empty lastObject] doubleValue] longitude:[[empty firstObject] doubleValue]];
                    regeo.requireExtension = YES;
                    regeo.radius = 1000;
                    [self.search AMapReGoecodeSearch:regeo];
                }else{
                    self.labelReGeocode.text =  @"";
                }
            }
        }else{
            self.labelReGeocode.text = entity.addressRoad;
        }
    }
}

-(UIView *)topView{
    if(!_topView){
        _topView  = [[UIView alloc]init];
        _topView.translatesAutoresizingMaskIntoConstraints = NO;
    }
    return _topView;
}
-(UIView *)lineView{
    if(!_lineView){
        _lineView  = [[UIView alloc]init];
        _lineView.backgroundColor = [UIColor colorWithRed:231/255.0f green:231/255.0f blue:231/255.0f alpha:1.0f];
    }
    return _lineView;
}
-(UIButton *)btnIcon{
    if(!_btnIcon){
        _btnIcon = [UIButton buttonWithType:UIButtonTypeCustom];
        [_btnIcon setImage:[UIImage imageNamed:@"icon-sendStatus-light"] forState:UIControlStateNormal];
        _btnIcon.userInteractionEnabled = NO;
        _btnIcon.translatesAutoresizingMaskIntoConstraints = NO;
    }
    return _btnIcon;
}

-(UILabel *)labelSendAddress{
    if(!_labelSendAddress){
        _labelSendAddress = [[UILabel alloc]init];
        _labelSendAddress.font = [UIFont systemFontOfSize:14.f];
        _labelSendAddress.lineBreakMode = NSLineBreakByCharWrapping;
        _labelSendAddress.numberOfLines = 0;
        _labelSendAddress.translatesAutoresizingMaskIntoConstraints = NO;
    }
    return _labelSendAddress;
}

-(TTTAttributedLabel *)labelReGeocode{
    if(!_labelReGeocode){
        _labelReGeocode = [[TTTAttributedLabel alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 20)];
        _labelReGeocode.textColor = [UIColor grayColor];
        _labelReGeocode.font = [UIFont systemFontOfSize:12.f];
        _labelReGeocode.lineBreakMode = NSLineBreakByCharWrapping;
        _labelReGeocode.numberOfLines = 0;
        _labelReGeocode.translatesAutoresizingMaskIntoConstraints = NO;
        _labelReGeocode.verticalAlignment=TTTAttributedLabelVerticalAlignmentTop;

    }
    return _labelReGeocode;
}

-(TTTAttributedLabel *)remarkLabel{
    if(!_remarkLabel){
        _remarkLabel = [[TTTAttributedLabel alloc]initWithFrame:CGRectZero];
        _remarkLabel.textColor = [UIColor grayColor];
        _remarkLabel.font = [UIFont systemFontOfSize:12.f];
        _remarkLabel.lineBreakMode = NSLineBreakByCharWrapping;
        _remarkLabel.numberOfLines = 0;
        _remarkLabel.verticalAlignment=TTTAttributedLabelVerticalAlignmentTop;
        
    }
    return _remarkLabel;
}

-(UIView *)bottomView{
    if(!_bottomView){
        _bottomView = [[UIView alloc]init];
        _bottomView.translatesAutoresizingMaskIntoConstraints = NO;
    }
    return _bottomView;
}

-(UIButton *)btnRush{
    if(!_btnRush){
        _btnRush = [UIButton buttonWithType:UIButtonTypeCustom];
        _btnRush.backgroundColor = getColor(@"27B088");
        [_btnRush setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//        [_btnRush setTitle:@"帮你买" forState:UIControlStateNormal];
        _btnRush.layer.masksToBounds = YES;
        _btnRush.layer.cornerRadius = 5.f;
        [_btnRush addTarget:self action:@selector(grabOneTouch:) forControlEvents:UIControlEventTouchUpInside];
        _btnRush.translatesAutoresizingMaskIntoConstraints = NO;
    }
    return _btnRush;
}

-(UIButton *)btnRob{
    
    
    if (!_btnRob) {
        _btnRob = [UIButton buttonWithType:UIButtonTypeCustom];
        _btnRob.backgroundColor = getColor(@"27B088");
        [_btnRob setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _btnRob.layer.masksToBounds = YES;
        _btnRob.layer.cornerRadius = 5.f;
        [_btnRob addTarget:self action:@selector(robOrderTouch:) forControlEvents:UIControlEventTouchUpInside];
        _btnRob.translatesAutoresizingMaskIntoConstraints = NO;
    }
    return _btnRob;
    
}

-(AMapSearchAPI *)search{
    if(!_search){
        _search = [[AMapSearchAPI alloc] init];
        _search.delegate = self;
    }
    return _search;
}
-(UILabel *)labelDistance{
    if(!_labelDistance){
        _labelDistance = [[UILabel alloc]init];
        _labelDistance.font = [UIFont systemFontOfSize:12.f];
        _labelDistance.textColor =  [UIColor colorWithRed:113/255.f green:113/255.f blue:113/255.f alpha:1.0];
        _labelDistance.textAlignment = NSTextAlignmentRight;

//        _labelDistance.backgroundColor = getColor(blueColor);
    }
    return _labelDistance;
}


-(UIButton *)addressIcon{
    if(!_addressIcon){
        _addressIcon = [UIButton buttonWithType:UIButtonTypeCustom];
        [_addressIcon setImage:[UIImage imageNamed:@"icon_address"] forState:UIControlStateNormal];
        [_addressIcon addTarget:self action:@selector(addressBtnTouch) forControlEvents:UIControlEventTouchUpInside];
    }
    return _addressIcon;
}

-(void)addressBtnTouch{
    
    if (self.addressBlock) {
        
        self.addressBlock(self.isWaimOrPaot, self.waiMaiDodelHList, self.waiMaiDodelList);
    }
}



@end
